package com.login.service.user;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.login.bean.Buyer;
import com.login.dao.impl.BuyerDaoImpl;
import com.login.dao.impl.SalerDaoImpl;

/**
 * Servlet implementation class RegistServlet
 */
@WebServlet("/RegistServlet")
public class RegistServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	BuyerDaoImpl registImpl=new BuyerDaoImpl();  
	SalerDaoImpl registSaler=new SalerDaoImpl();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegistServlet() {
        super();
      
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String username = request.getParameter("regName");
		String userpwd =request.getParameter("regPwd");
		String phoneNumber = request.getParameter("phoneNumber");
		String identify =request.getParameter("typeUser");
		System.out.println(identify);
		Buyer user = new Buyer();
		user.setName(username);
		user.setPwd(userpwd);
		user.setIdentify(identify);
		user.setPhoneNumber(phoneNumber);
		Date currentTime=new Date();
		String time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(currentTime);
		int regist=0;
		if(identify.equals("buyer")){
			regist=registImpl.saveBuyer(user,time);
				if(regist!=0){
					System.out.println("注册成功！");
					response.sendRedirect("login.jsp");
					}
				else{
					//response.getWriter().println("");
					response.sendRedirect("regist.jsp");
					}
			}
		else if(identify.equals("seller")){
			regist=registSaler.saveSeller(user, time);			
				if(regist!=0){
					response.sendRedirect("login.jsp");
				}
				else{
					//response.getWriter().println("");
					response.sendRedirect("regist.jsp");
					}
			}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
